<template>
  <div class="app-container">

    <basic-query-form ref="queryForm" @doQuery="getList"  @resetQuery="getList" :dept-type="2" >
      <template v-slot:queryItem="data">
        <el-form-item label="员工姓名" >
          <el-input
            v-model="data.queryParams.userName"
            placeholder="请输入员工姓名"
            clearable
            size="small"
          />
        </el-form-item>
      </template>
    </basic-query-form>


    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['kh:person:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['kh:person:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['kh:person:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport"
          v-hasPermi="['system:user:import']"
        >导入</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['kh:person:export']"
        >导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="personList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="被考核人id" align="center" prop="id" />-->
      <el-table-column label="员工姓名" align="center" prop="userName" />
      <el-table-column label="性别" align="center" prop="sex" :formatter="sexFormat"/>
      <el-table-column label="所属部门" align="center" prop="deptId" :formatter="deptFormat" />
      <el-table-column label="所属考核计划" align="center" prop="assessmentId" :formatter="assessFormat"/>
      <el-table-column label="所属考核方案" align="center" prop="programmeId" :formatter="programmeFormat" />
      <el-table-column label="出生日期" align="center" prop="birthDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.birthDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="原职务" align="center" prop="originalPosition" />
      <el-table-column label="现职务" align="center" prop="presentPosition" />
      <el-table-column label="任职时间" align="center" prop="positionTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.positionTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="职级" align="center" prop="postName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['kh:person:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['kh:person:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getListByQueryForm"
    />

    <!-- 添加或修改被考核人信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="员工姓名" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入员工姓名" />
        </el-form-item>

        <el-form-item label="用户性别" prop="sex">
          <el-select v-model="form.sex" placeholder="请选择">
            <el-option
              v-for="dict in sexOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="出生日期" prop="birthDate">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.birthDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择出生日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="原职务" prop="originalPosition">
          <el-input v-model="form.originalPosition" placeholder="请输入原职务" />
        </el-form-item>
        <el-form-item label="现职务" prop="presentPosition">
          <el-input v-model="form.presentPosition" placeholder="请输入现职务" />
        </el-form-item>
        <el-form-item label="任职时间" prop="positionTime">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.positionTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择任职时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="职级" prop="postName">
          <el-input v-model="form.postName" placeholder="请输入现职级" />
        </el-form-item>
        <el-form-item label="所属考核计划">
          <el-select v-model="form.assessmentId"  @change="getSubProgramm" placeholder="请选择" v-bind:disabled="form.id != undefined">
            <el-option
              v-for="item in AssessmentOptions"
              :key="item.value"
              :label="item.name"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所属考核方案">
          <el-select v-model="form.programmeId"  placeholder="请选择" v-bind:disabled="form.id != undefined">
            <el-option
              v-for="item in ProgrammeOptions"
              :key="item.value"
              :label="item.name"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所属部门">
          <el-select v-model="form.deptId"  placeholder="请选择" v-bind:disabled="form.id != undefined">
            <el-option
              v-for="item in deptOptions"
              :key="item.value"
              :label="item.name"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <import-form  ref="khPersonImport" :open="false" title="被考核对象导入" :upload-url="uploadUrl" :update-support="true"
                  @doQuery="getListByQueryForm"

    >

      <template v-slot:tip>
        <div class="el-upload__tip" style="color:red" slot="tip">提示：仅允许导入“xlsx”格式文件！</div>
        <div class="el-upload__tip" style="color:red" slot="tip">不在考核计划里的公司所属的领导或新选拔干部不会被导入</div>
      </template>
    </import-form>
  </div>
</template>

<script>
import { listPerson, getPerson, delPerson, addPerson, updatePerson, exportPerson } from "@/api/kh/person";
import BasicQueryForm from '../../components/BasicQueryForm'
import ImportForm from '../../components/ImportForm'

export default {
  name: "Person",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 被考核人信息表格数据
      personList: [],
      // 性别状态字典
      sexOptions: [],
      positionOptions:[],
      //考核列表
      AssessmentOptions:[],
      //考核列表
      ProgrammeOptions:[],
      //被考核部门
      deptOptions:[],
      programmeId:'',
      assessmentId:'',
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
    };
  },
  computed: {
    uploadUrl() {
      return "/kh/person/importData/" + this.programmeId;
    }
  },
  components: {
    'BasicQueryForm' : BasicQueryForm,
    'ImportForm' : ImportForm
  },
  created() {
    this.programmeId = this.$route.params.programmeId;
    this.assessmentId = this.$route.params.assessmentId;
    this.getDicts("sys_user_sex").then(response => {
      this.sexOptions = response.data;
    });
  },
  methods: {
    getListByQueryForm(){
      this.$refs.queryForm.pageQuery(this.queryParams)
    },
    getCurAssess(row){
      return this.AssessmentOptions.find(item => item.value === row.assessmentId);
    },
    deptFormat(row,column){
      return this.getCurAssess(row).depts.find(item => item.value === row.deptId).name;
    },
    assessFormat(row,column){
      return this.getCurAssess(row).name;
    },
    programmeFormat(row,column){
      return this.getCurAssess(row).programmes.find(item => item.value === row.programmeId).name;
    },

    // 性别字典翻译
    sexFormat(row, column) {
      return this.selectDictLabel(this.sexOptions, row.sex);
    },
    /** 查询被考核人信息列表 */
    getList(queryParams) {
      this.queryParams.pageNum = queryParams.pageNum;
      this.queryParams.pageSize = queryParams.pageSize;
      this.loading = true;
      listPerson(queryParams).then(response => {
        this.personList = response.rows;
        this.total = response.total;
        this.AssessmentOptions = this.$refs.queryForm.AssessmentOptions;
        this.loading = false;
      });
    },
    getSubProgramm(){
      let curAssess = this.AssessmentOptions.find(item=> item.value === this.form.assessmentId);
      if(!curAssess) return;
      this.ProgrammeOptions = curAssess.programmes;
      this.deptOptions = curAssess.depts;
      this.form.programmeId = undefined;
      this.form.deptId = undefined;
      this.form.khPersonId = undefined;
    },
    setByAssess(){
      let curAssess = this.AssessmentOptions.find(item=> item.value === this.form.assessmentId);
      if(!curAssess) return;
      this.ProgrammeOptions = curAssess.programmes;
      this.deptOptions = curAssess.depts;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        userName: undefined,
        deptId: undefined,
        sex: undefined,
        assessmentId: undefined,
        programmeId: undefined,
        birthDate: undefined,
        originalPosition: undefined,
        presentPosition: undefined,
        positionTime: undefined,
        postCode: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined
      };
      this.resetForm("form");
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.form.sex = 0;
      this.form.deptId = this.queryParams.deptId;
      this.form.assessmentId = this.queryParams.assessmentId;
      this.form.programmeId = this.queryParams.programmeId;
      this.open = true;
      this.title = "添加被考核人信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getPerson(id).then(response => {
        this.form = response.data;
        this.setByAssess();
        this.open = true;
        this.title = "修改被考核人信息";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updatePerson(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getListByQueryForm();
              } else {
                this.msgError(response.msg);
              }
            });
          } else {
            addPerson(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getListByQueryForm();
              } else {
                this.msgError(response.msg);
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      let rowsDeleted = this.$CrudUtils.rowsDeleted(
        row, this.ids, this.personList, o=>o.id, o=>o.userName
      )

      this.$confirm('是否确认删除被考核人信息[ ' + rowsDeleted.msgs + ' ]的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delPerson(rowsDeleted.ids);
        }).then(() => {
          this.getListByQueryForm();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导入按钮操作 */
    handleImport() {
      this.$refs.khPersonImport.show();
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有被考核人信息数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportPerson(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>
